package com.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.Config;
import com.db.DB;
import com.db.Event;
import com.db.Friends;
import com.db.Reply;
import com.db.User;
import com.db.UserInfo;
import com.tools.Constants;
import com.tools.Files;

/**
 * @author lemonjesnme
 * @category init tables
 */
public class C2S0init extends HttpServlet {
	@Override
	public void init() throws ServletException {
		Constants.init();
		System.out.println("DB name:"+Constants.DBNAME);
		DB.connDB();
		super.init();
	}

	private static final long serialVersionUID = 1L;
	private HashMap<String, String> friendsParam = new HashMap<String, String>();

	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		Enumeration<String> er = request.getParameterNames();
		System.out.println("[vswb] accept the data begin");
		long user_id = 1;
		
		/**
		 * init db,if the use is first login in,into login view ,not goto list_friends view.
		 */
		try {
			ResultSet rs = DB.select("select isTomFirst from Config;");
			while (rs.next()) {
				if (rs.getString("isTomFirst").equals("true")) {
					try {
						DB.dropTable("User");
						DB.dropTable("Friends");
						DB.dropTable("Config");
						DB.dropTable("UserInfo");
						DB.dropTable("Event");
						DB.dropTable("Reply");
						DB.createTable(User.class);
						DB.createTable(Friends.class);
						DB.createTable(Config.class);
						DB.createTable(UserInfo.class);
						DB.createTable(Event.class);
						DB.createTable(Reply.class);
					} catch (Exception e) {
						System.out.println("[vswb] clean db error.");
					}
					friendsParam.put("phone_number", "0");
					DB.insert("User", "_id", "0", friendsParam);
					friendsParam.clear();
					friendsParam.put("_id", "0");
					friendsParam.put("name", "system");
					friendsParam.put("phone_number", "0");
					friendsParam.put("reply", "");
					DB.insert("Friends", "user_id", "0", friendsParam);
					DB.exeCmd("alter table Friends add time DATETIME");
					DB.exeCmd("update friends set time=now() where _id='0';");
					friendsParam.clear();
					friendsParam.put("user_id", "1");
					DB.insert("Config", "isTomFirst", "false", friendsParam);
					friendsParam.clear();
					friendsParam.put("my_story", "在这里开始,留下你的影子,记录你的心情吧...");
					friendsParam.put("img_path", "http://"+Constants.SERVER_ADDRESS+Constants.PORT+"/tom/res/img/0.jpg");
					DB.insert("UserInfo", "user_id", "0", friendsParam);
					DB.exeCmd("alter table userinfo add time DATETIME");
					DB.exeCmd("update userinfo set time=now() where user_id='0';");
					friendsParam.clear();
				}
			}
		} catch (Exception e) {
			try {
				DB.createTable(UserInfo.class);
				DB.createTable(User.class);
				DB.createTable(Friends.class);
				DB.createTable(Config.class);
				DB.createTable(Event.class);
				DB.createTable(Reply.class);
			} catch (Exception e1) {
				System.out.println("[vswb] clean db error.");
			}
			friendsParam.put("phone_number", "0");
			DB.insert("User", "_id", "0", friendsParam);
			friendsParam.clear();
			friendsParam.put("_id", "0");
			friendsParam.put("name", "system");
			friendsParam.put("phone_number", "0");
			friendsParam.put("reply", "");
			DB.insert("Friends", "user_id", "0", friendsParam);
			DB.exeCmd("alter table Friends add time DATETIME");
			DB.exeCmd("update friends set time=now() where _id='0';");
			friendsParam.clear();
			friendsParam.put("user_id", "1");
			DB.insert("Config", "isTomFirst", "false", friendsParam);
			friendsParam.clear();
			friendsParam.put("my_story", "在这里开始,留下你的影子,记录你的心情吧...");
			friendsParam.put("img_path", "http://"+Constants.SERVER_ADDRESS+Constants.PORT+"/tom/res/img/0.jpg");
			DB.insert("UserInfo", "user_id", "0", friendsParam);
			DB.exeCmd("alter table userinfo add time DATETIME");
			DB.exeCmd("update userinfo set time=now() where user_id='0';");
			friendsParam.clear();
		}
		try {
			ResultSet rs = DB.select("select user_id from Config;");
			while (rs.next()) {
				user_id =Long.valueOf(rs.getString("user_id"));
			}
		} catch (Exception e1) {

		}
		// response.setContentType("text/html");
		PrintWriter writer = response.getWriter();
		String phone_number = null;

		/**
		 * get phone number insert into server tables.
		 * in the first time ,insert a system user .
		 * user_id is screeming.
		 */
		while (er.hasMoreElements()) {
			String element = er.nextElement();
			if (element.equals("phone_number")) {
				phone_number = request.getParameterValues("phone_number")[0];
				HashMap userParam = new HashMap<String, String>();
				userParam.put("phone_number", phone_number);
				DB.insert("User", "_id", String.valueOf(user_id), userParam);
				writer.println("user_id:" + String.valueOf(user_id));
				System.out.println("[vswb] user_id:" + user_id + " phone_number:" + phone_number);
				friendsParam.put("_id", "0");
				friendsParam.put("name", "system");
				friendsParam.put("phone_number", "0");
				friendsParam.put("reply", "");
				DB.insert("Friends", "user_id", String.valueOf(user_id), friendsParam);
				DB.exeCmd("update friends set time=now() where user_id='"+user_id+"' and _id='0';");
				friendsParam.clear();
				friendsParam.put("user_id", "0");
				friendsParam.put("phone_number", phone_number);
				DB.insert("Friends", "_id", String.valueOf(user_id), friendsParam);
				DB.exeCmd("update friends set time=now() where user_id='0' and _id='"+user_id+"';");
				friendsParam.clear();
				friendsParam.put("my_story", "在这里开始,留下你的影子,记录你的心情吧... ");
				String imgPath = "http://" + Constants.SERVER_ADDRESS + Constants.PORT+"/tom/res/img/"+String.valueOf(user_id)+".jpg";
				try {
					Files.copyFile("C:\\workspace\\tom\\res\\img\\android.jpg", "C:\\workspace\\tom\\res\\img\\"+String.valueOf(user_id)+".jpg");
				} catch (Exception e) {
					e.printStackTrace();
				}
				friendsParam.put("img_path",imgPath);
				DB.insert("UserInfo","user_id",String.valueOf(user_id),friendsParam);
				DB.exeCmd("update userinfo set time=now() where user_id='"+user_id+"';");
				friendsParam.clear();
				System.out.println("[vswb] friends_id:0" + "belong:" + user_id);
			}
		}
		user_id++;
		DB.update("update Config set user_id='"+user_id+"';");
	}
}
